package com.projects.android.views;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.projects.android.R;
import com.projects.android.bus.CityBUS;
import com.projects.android.bus.FoodTypeBUS;
import com.projects.android.bus.RestaurantBUS;
import com.projects.android.dto.CityDTO;
import com.projects.android.dto.DistrictDTO;
import com.projects.android.dto.FoodTypeDTO;
import com.projects.android.dto.RestaurantDTO;
import com.projects.android.utils.SelectedItems;
import com.projects.android.utils.Utils;

public class Order extends Activity implements OnItemSelectedListener,
		OnClickListener {
	private Spinner lstCity;
	private Spinner lstDistrict;
	private Spinner lstFoodType;
	private Intent intent;
	private Button btnViewRestaurants;
	@SuppressWarnings("rawtypes")
	private ArrayAdapter spinnerCityAdapter;
	@SuppressWarnings("rawtypes")
	private ArrayAdapter spinnerDistrictAdapter;
	@SuppressWarnings("rawtypes")
	private ArrayAdapter spinnerFoodTypeAdapter;
	private ArrayList<CityDTO> arrCity;
	private ArrayList<DistrictDTO> arrDistrict;
	private ArrayList<FoodTypeDTO> arrFoodType;

	// Use an existing ListAdapter that will map an array
	// of strings to TextViews
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.order);

		btnViewRestaurants = (Button) findViewById(R.id.btnViewRestaurants);
		lstCity = (Spinner) findViewById(R.id.lstCity);
		lstDistrict = (Spinner) findViewById(R.id.lstDistrict);
		lstFoodType = (Spinner) findViewById(R.id.lstFoodType);

		// Load all cities
		arrCity = CityBUS.selectAll();
		if (arrCity != null) {
			spinnerCityAdapter = new ArrayAdapter(this,
					android.R.layout.simple_spinner_dropdown_item, arrCity);
			lstCity.setAdapter(spinnerCityAdapter);
		}

		// Load all food types
		arrFoodType = FoodTypeBUS.selectAll();
		if (arrFoodType != null) {
			spinnerFoodTypeAdapter = new ArrayAdapter(this,
					android.R.layout.simple_spinner_dropdown_item, arrFoodType);
			lstFoodType.setAdapter(spinnerFoodTypeAdapter);
		}

		// Register event listener
		lstCity.setOnItemSelectedListener(this);
		lstDistrict.setOnItemSelectedListener(this);
		lstFoodType.setOnItemSelectedListener(this);
		btnViewRestaurants.setOnClickListener(this);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
		// TODO Auto-generated method stub
		switch (parent.getId()) {
		case R.id.lstCity:
			if (pos >= 0 && pos < arrCity.size()) {
				SelectedItems.SelectedCity = arrCity.get(pos);
				arrDistrict = SelectedItems.SelectedCity.getDistricts();
				if (arrDistrict != null) {
					spinnerDistrictAdapter = new ArrayAdapter(this,
							android.R.layout.simple_spinner_dropdown_item,
							arrDistrict);
					lstDistrict.setAdapter(spinnerDistrictAdapter);
				}
			}
			break;
		case R.id.lstDistrict:
			if (pos >= 0 && pos < arrDistrict.size()) {
				SelectedItems.SelectedDistrict = arrDistrict.get(pos);
			}
			break;
		case R.id.lstFoodType:
			if (pos >= 0 && pos < arrFoodType.size()) {
				SelectedItems.SelectedFoodtype = arrFoodType.get(pos);
			}
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> paramAdapterView) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnViewRestaurants:
			loadRestaurant();
		}
	}

	private void loadRestaurant() {
		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("Loading. Please wait...");
		dialog.setIndeterminate(true);

		final Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				runOnUiThread(new Runnable() {
					ArrayList<RestaurantDTO> data = RestaurantBUS
							.selectByIDDistrictAndIDFoodType(
									SelectedItems.SelectedDistrict.getID(),
									SelectedItems.SelectedFoodtype.getID());

					@Override
					public void run() {
						// Transfer data for activity
						ListRestaurant.Restaurants = data;
						// Start activity
						intent = Utils.browseIntent(Order.this,
								"ListRestaurant");
						startActivity(intent);
						dialog.dismiss();
					}
				});
			}
		});
		dialog.show();
		thread.start();
	}
}
